package com.example.mapp;

import com.example.entity.authority.Party;
import com.example.ext.entity.ExtParty;
import com.example.mapp.buildSql.PartyMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/9/9.
 */
@Mapper
public interface PartyMapp {


    @SelectProvider(type = PartyMappSql.class,method = "queryList")
    List<ExtParty> queryList(ExtParty extParty) throws Exception;

    @SelectProvider(type = PartyMappSql.class,method = "queryRandomList")
    List<Map> queryRandomList(Map map) throws Exception;

    @Select("select p.*,pp.party_name as parentName" +
            " from party p left join (select id,party_name from party) pp on pp.id = p.parent_id where p.id=#{id}")
    ExtParty queryById(String id) throws Exception;

    @Select("select id,party_name, party_addr,party_type,parent_id from party where party_name=#{name}")
    Party queryByName(String name) throws  Exception;

    @Select("select id,party_name,party_addr,party_type,parent_id from party where party_name like #{name}")
    List<Party> queryByNameLk(String name) throws  Exception;

    @Insert("insert into party (party_Name,party_Province,party_City,party_Area,party_Addr," +
            "corporation,telephone,postal_Code,credit_Code,industry,contacts,contacts_Work," +
            "contacts_Card,contacts_Phone,contacts_Emile,contacts_Mobile,contacts_Address,business_License_Img," +
            "parent_Id,party_type,business_License_Num,zzzs,zzdj,maint_start) values" +
            " (#{partyName},#{partyProvince},#{partyCity},#{partyArea},#{partyAddr},#{corporation}," +
            "#{telephone},#{postalCode},#{creditCode},#{industry},#{contacts},#{contactsWork},#{contactsCard}," +
            "#{contactsPhone},#{contactsEmile},#{contactsMobile},#{contactsAddress},#{businessLicenseImg}," +
            "#{parentId},#{partyType},#{businessLicenseNum},#{zzzs},#{zzdj},#{maintStart})")
    @SelectKey(statement="select last_insert_id()",keyProperty = "id",keyColumn = "id",before = false,resultType = Long.class)
    int insert(Party party) throws Exception;

    @Update("update party set party_Name=#{partyName},party_Province=#{partyProvince},party_City=#{partyCity}," +
            "party_Area=#{partyArea},party_Addr=#{partyAddr},corporation=#{corporation},telephone=#{telephone}" +
            ",postal_Code=#{postalCode},credit_Code=#{creditCode},industry=#{industry},contacts=#{contacts}" +
            ",contacts_Work=#{contactsWork},contacts_Card=#{contactsCard},contacts_Phone=#{contactsPhone}," +
            "contacts_Emile=#{contactsEmile},contacts_Mobile=#{contactsMobile},contacts_Address=#{contactsAddress}," +
            "business_License_Img=#{businessLicenseImg},parent_Id=#{parentId},party_type=#{partyType}, " +
            "business_License_Num=#{businessLicenseNum},zzzs=#{zzzs},zzdj=#{zzdj},maint_start=#{maintStart} where id = #{id}")
    int update(Party party) throws Exception;

    @Delete(" delete from party where id = #{id}")
    int deleteById(String id) throws Exception;

    @Select(" select * from party where parent_id =#{id}")
    List<Party> queryByPartyIdList(String id) throws Exception;

    @Select(" select * from party where parent_id =#{id} and party_type=#{partyType}")
    List<Party> queryByIdAndTypeList(Party party) throws Exception;
    @Select(" select p1.* from party p1 where p1.party_type=#{partyType} and exists (select 1 from party p2 where p2.parent_id=p1.id and p2.party_type in ('1','2','3'))")
    List<Party> queryByLastManageAndTypeList(String partyType) throws Exception;
    // 通过公司类型查询所有信息
    @Select(" select * from party where party_type=#{partyType} and party_name is not null")
    List<Party> queryByTypeList(String partyType) throws Exception;


    @Select("select * from party where  party_type=#{partyType} and party_name=#{partyName}")
    Party queryByPartyTypeAndName(Party party) throws Exception;

    @Select("select  a.*  from party as a " +
            "INNER join party_user as b on a.id = b.user_id " +
            "inner join party as c on b.party_id = c.id where b.user_id =#{id} ")
    Party queryBySysUserId(String id) throws Exception;

    @Select("select  a.*  from party as a " +
            "INNER join party_user as b on a.id = b.user_id " +
            "inner join party as c on b.party_id = c.id where a.contacts_emile =#{email} ")
    List<Party> queryByEmail(String email) throws Exception;


    @Select("select * from party where parent_id=#{id} and party_type = '4'")
    List<Party> decideBranchInfo(String id);

    @Select("select * from party where parent_id in (select id from party where parent_id=#{id} and party_type='4')")
    List<Party> decideForEach(String id);

    @Select("select * from party where id=#{id}")
    Party infoById(String id);


    /*获取数量*/
    @Select("select SUM(CASE WHEN IS_STOP IN ('0', '1') THEN 1 ELSE 0 END) elevatorNum," +
            " SUM(CASE WHEN IS_STOP = '1' THEN 1 ELSE 0 END)         eleStopNum," +
            " SUM(CASE WHEN IS_STOP = '0' THEN 1 ELSE 0 END)         eleUsedNum" +
            " from party p left join ELEVATOR r ON p.id=r.branch_office where p.parent_id=#{id} and party_type='4' ")
    Map<String, Object> querysum(Long id);

    /*获取数量*/
    @Select("select SUM(CASE WHEN IS_STOP IN ('0', '1') THEN 1 ELSE 0 END) elevatorNum," +
            " SUM(CASE WHEN IS_STOP = '1' THEN 1 ELSE 0 END)         eleStopNum," +
            " SUM(CASE WHEN IS_STOP = '0' THEN 1 ELSE 0 END)         eleUsedNum" +
            " from party p left join ELEVATOR r ON p.id=r.branch_office where p.id=#{id} and party_type='4' ")
    Map<String, Object> querysumInId(Long id);

    /**
     * 获取某个单位下某个角色的用户
     * @param map
     * @return
     * @throws Exception
     */
    @Select("SELECT DISTINCT CONCAT(C.USER_ID) userId FROM party A "
            + " INNER JOIN PARTY_USER B ON A.ID=B.PARTY_ID "
            + " INNER JOIN SYS_USER_ROLE C ON C.USER_ID=B.USER_ID "
            + " INNER JOIN SYSTEM_ROLE D ON C.ROLE_ID=D.ID "
            + " WHERE D.ROLE_NAME LIKE #{roleName} "
            + " AND B.party_id = #{partyId}")
    List<String> queryUserList(Map<String, Object> map) throws Exception;

    /**
     * 根据单位级别获取对应监管单位
     * @param partyLevel
     * @return
     * @throws Exception
     */
    @Select("select A.party_name as partyName,A.ID,A.parent_id as parentId from party A WHERE party_level = #{partyLevel};")
    List<Party> queryPartyByPartyLevel(String partyLevel) throws Exception;

}
